package com.dha.asynctask;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.dha.adapter.ThongBaoAdapter;
import com.dha.caodangcongnghe.R;
import com.dha.model.ThongBaoModel;
import com.dha.variable.ThongBaoVariable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;

public class XemThongBaoAsyncTask extends AsyncTask<Void, String, Void> {
	private ProgressDialog dialog;

	private Activity activity;

	private ListView listView;

	private ArrayList<ThongBaoModel> arr;

	private ThongBaoAdapter adapter;

	private String maGiaoVien = null, ngayDang = null;
	// cái này để chuyển từ dạng "yyyy-MM-dd'T'HH:mm:ss" bên sql qua
	private SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ss");
	// // cái này để chuyển sang dạng mong muốn
	private SimpleDateFormat dateOutput = new SimpleDateFormat(
			"HH:mm - dd/MM/yyyy");

	public XemThongBaoAsyncTask(Activity activity, String maGiaoVien,
			String ngayDang) {
		super();
		this.activity = activity;
		listView = (ListView) activity.findViewById(R.id.lv_ThongBao);
		arr = new ArrayList<ThongBaoModel>();

		adapter = new ThongBaoAdapter(activity,
				R.layout.layout_thongbao_custom, arr);

		listView.setAdapter(adapter);

		this.maGiaoVien = maGiaoVien;
		this.ngayDang = ngayDang;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang tải thông báo về");
		super.onPreExecute();
	}

	@Override
	protected Void doInBackground(Void... params) {
		String url = ThongBaoVariable.URL;
		String namespace = ThongBaoVariable.NAME_SPACE;
		String methodname = null;
		SoapSerializationEnvelope envelope = null;
		try {
			if (maGiaoVien.equals(null) && ngayDang.equals(null)) {
				//xem khong dieu kien
				methodname = ThongBaoVariable.METHOD_NAME_XEMTHONGBAO;
			} else {
				if (ngayDang.equals(null)) {
					//xem theo giao vien
					methodname = ThongBaoVariable.METHOD_NAME_XEMTHONGBAO_MAGIAOVIEN;
				}else{
					if (maGiaoVien.equals(null)){
						//xem theo ngay
						methodname = ThongBaoVariable.METHOD_NAME_XEM_THONG_BAO_THEO_NGAY;
					}else{
						//xem ca 2
						methodname = ThongBaoVariable.METHOD_NAME_XEM_THONG_BAO_THEO_NGAY_VA_GIAO_VIEN;
					}
				}
			}
			String soapAction = namespace + methodname;
			SoapObject request = new SoapObject(namespace, methodname);
			//doi so ma giao vien
			PropertyInfo maGiaoVienInfo = new PropertyInfo();
			maGiaoVienInfo.setName("MaGiaoVien");
			maGiaoVienInfo.setValue(maGiaoVien);
			request.addProperty(maGiaoVienInfo);
			//doi so ngay dang
			PropertyInfo ngayDangInfo = new PropertyInfo();
			ngayDangInfo.setName("Ngay");
			ngayDangInfo.setValue(ngayDang);
			request.addProperty(ngayDangInfo);
			//
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTransportSE = new HttpTransportSE(url);
			httpTransportSE.call(soapAction, envelope);
			Vector<SoapObject> respone = (Vector<SoapObject>) envelope
					.getResponse();
			if (respone != null) {
				for (SoapObject so : respone) {
					publishProgress(so.getProperty("tieuDe").toString(), so
							.getProperty("noiDung").toString(),
							so.getProperty("tenGiaoVien").toString(), so
									.getProperty("ngayDang").toString());
				}
			}
			// neu co gia tri ma giao vien la null
			// co nghia la ma giao vien null
			// lam vie tai thong bao full ve
		} catch (NullPointerException n) {
			try {
				methodname = ThongBaoVariable.METHOD_NAME_XEMTHONGBAO;
				String soapAction = namespace + methodname;
				SoapObject request = new SoapObject(namespace, methodname);
				envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.setOutputSoapObject(request);

				HttpTransportSE httpTransportSE = new HttpTransportSE(url);
				httpTransportSE.call(soapAction, envelope);
				Log.i("hehe", "abc");
				Vector<SoapObject> respone = (Vector<SoapObject>) envelope
						.getResponse();
				if (respone != null) {
					for (SoapObject so : respone) {
						publishProgress(so.getProperty("tieuDe").toString(), so
								.getProperty("noiDung").toString(), so
								.getProperty("tenGiaoVien").toString(), so
								.getProperty("ngayDang").toString());
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			// neu co 1 thong bao
		} catch (java.lang.ClassCastException ex) {
			try {
				SoapObject so = (SoapObject) envelope.getResponse();
				publishProgress(so.getProperty("tieuDe").toString(), so
						.getProperty("noiDung").toString(),
						so.getProperty("tenGiaoVien").toString(), so
								.getProperty("ngayDang").toString());
			} catch (SoapFault e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			Log.i("hehe", "thong bao " + e.toString());
		}

		return null;
	}

	@Override
	protected void onProgressUpdate(String... values) {
		String tieuDe = values[0].toString();
		String noiDung = values[1].toString();
		String nguoiDang = values[2].toString();
		String ngayDangTuSQL = values[3].toString();
		String ngayDangInRa = null;
		try {
			// Chuyển dạng tring lấy ra quên sql, service qua dạng ngày
			Date date = dateFormat.parse(ngayDangTuSQL.substring(0, 19));
			// chuyển ngược về lại dạng mong muốn
			ngayDangInRa = dateOutput.format(date);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			Log.i("hehe", e1.toString());
		}

		ThongBaoModel tb = new ThongBaoModel(tieuDe, noiDung, nguoiDang,
				ngayDangInRa);

		arr.add(tb);

		// adapter.notifyDataSetChanged();

		super.onProgressUpdate(values);
	}

	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		adapter.notifyDataSetChanged();
		dialog.dismiss();
		super.onPostExecute(result);
	}

}
